package icu.d4peng.cloud.common.mvc;

import com.fasterxml.jackson.datatype.jsr310.ser.LocalDateTimeSerializer;
import icu.d4peng.cloud.common.mvc.controller.DefaultErrorController;
import icu.d4peng.cloud.common.mvc.properties.MvcProperties;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.AutoConfigureBefore;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.autoconfigure.jackson.Jackson2ObjectMapperBuilderCustomizer;
import org.springframework.boot.autoconfigure.web.servlet.WebMvcAutoConfiguration;
import org.springframework.boot.autoconfigure.web.servlet.error.ErrorMvcAutoConfiguration;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.boot.web.servlet.error.DefaultErrorAttributes;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

/**
 * @author d4peng
 * @version 1.0.0
 * @date 2021-12-30 11:26
 * @description MvcAutoConfiguration:默认MVC处理机制
 */
@Configuration
@ConditionalOnProperty(prefix = MvcProperties.PREFIX, value = {"enabled"}, matchIfMissing = true, havingValue = "true")
@EnableConfigurationProperties({MvcProperties.class})
@AutoConfigureBefore({WebMvcAutoConfiguration.class, ErrorMvcAutoConfiguration.class})
public class MvcAutoConfiguration {
    private static final Logger LOGGER = LoggerFactory.getLogger(MvcAutoConfiguration.class);

    @ConditionalOnProperty(prefix = MvcProperties.PREFIX, value = {"enable-default-errors-handler"}, matchIfMissing = true, havingValue = "true")
    @ConditionalOnMissingBean({DefaultErrorController.class})
    @Bean
    public DefaultErrorController defaultErrorController() {
        return new DefaultErrorController(new DefaultErrorAttributes());
    }

    @Bean
    @Autowired
    public Jackson2ObjectMapperBuilderCustomizer jackson2ObjectMapperBuilderCustomizer(MvcProperties mvcProperties) {
        DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern(mvcProperties.getGlobalTimeFormatRegex());
        return builder -> builder.serializerByType(LocalDateTime.class, new LocalDateTimeSerializer(dateTimeFormatter));
    }
}